package cn.rxls.community.service.impl;

import cn.rxls.community.dto.region.RegionIndexPageDTO;
import cn.rxls.community.entity.Region;
import cn.rxls.community.mapper.RegionMapper;
import cn.rxls.community.service.RegionService;
import cn.rxls.community.vo.region.RegionHomeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 区域(Region)表服务实现类
 *
 */
@Service("regionService")
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    private final RegionMapper regionMapper;

    RegionServiceImpl(RegionMapper regionMapper) {
        this.regionMapper = regionMapper;
    }


    /**
     * 查询所有的地域或社区
     *
     * @param district 区域名称
     * @return 区域或社区名称列表
     */
    @Override
    public List<String> selectAllDistrictCommunity(String district) {
        LambdaQueryWrapper<Region> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Region::getDistrict).groupBy(Region::getDistrict);
        if (Objects.nonNull(district) && !district.equals("")) {
            wrapper.clear();
            wrapper.eq(Region::getDistrict, district).select(Region::getPlace).groupBy(Region::getPlace);
            List<Region> regions = regionMapper.selectList(wrapper);
            return regions.stream().map(Region::getPlace).collect(Collectors.toList());
        }
        List<Region> regions = regionMapper.selectList(wrapper);
        return regions.stream().map(Region::getDistrict).collect(Collectors.toList());
    }

    /**
     * 分页查询指定区域的房屋数据
     *
     * @param pageDTO 参数悐
     * @return 多条社区房屋信息
     */
    @Override
    public List<RegionHomeVO> selectPageRegion(RegionIndexPageDTO pageDTO) {
        //对分页参数进行处理
        pageDTO.setPage((pageDTO.getPage() - 1) * pageDTO.getSize());
        Region region = new Region();
        //处理所需的查询条件
        BeanUtils.copyProperties(pageDTO, region);
        //查询出数据
        List<Map<String, Object>> maps = regionMapper.selectRegionPage(pageDTO.getPage(), pageDTO.getSize(), region);

        //准备返回的数据对象
        List<RegionHomeVO> lists = new ArrayList<>();

        maps.forEach(s -> {
            if (Objects.nonNull(s.get("price")) && Objects.nonNull(s.get("discount"))) {
                RegionHomeVO item = new RegionHomeVO();
                item.setRegionId(Long.valueOf(String.valueOf(s.get("regionId"))));
                item.setVillage(String.valueOf(s.get("village")));
                item.setCommunity(String.valueOf(s.get("community")));
                item.setPrice(Long.valueOf(String.valueOf(s.get("price"))));
                item.setDiscount(Long.valueOf(String.valueOf(s.get("discount"))));
                item.setImage(String.valueOf(s.get("regionImg")).split(","));
                item.setStreet(String.valueOf(s.get("street")));
                lists.add(item);
            }
        });
        return lists;
    }
}

